<?php

namespace App\Http\Utils\Integral;

use Illuminate\Support\Facades\Cache;

/**
 * Class UserContainer
 * @package App\Http\Utils\Integral
 */
trait UserContainer
{
    /**
     * 用户列表
     * @var User[]
     */
    public $users = [];

    /**
     * 获取用户
     * @param string $userID
     * @param string $shopId
     * @return User
     */
    public function user($userID, $shopId)
    {
        if (!array_key_exists(sprintf("%s_%s", $userID, $shopId), $this->users)) {
            $this->users[sprintf("%s_%s", $userID, $shopId)] = new User($userID, $shopId);
        }
        return $this->users[sprintf("%s_%s", $userID, $shopId)];
    }

    /**
     * 记录
     * @param bool $needPush
     */
    public function userRecord($needPush = false)
    {
        if ($needPush) {
            $pushData = [];
            foreach ($this->users as $user) {
                $user->record();
                $pushData = array_merge($pushData, $user->flows);
            }
            Cache::put('Template_Message', $pushData, 60 * 24);
        } else {
            foreach ($this->users as $user) {
                $user->record();
            }
        }
    }
}